﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class ViewList : System.Windows.Forms.Form
	{
		public ViewList(List<Autodesk.Revit.DB.View> viewList, Autodesk.Revit.DB.View originView)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Location = new System.Drawing.Point(100, 100);
			base.Icon = Resource.YArchitechIcon;
			this.thisViewList = viewList;
			this.thisOriginView = originView;
			this.selectedViews = new List<Autodesk.Revit.DB.View>();
			this.BindCheckboxList();
			this.isInit = true;
			bool @checked;
			bool checked2;
			this.CheckSelectedOption(out @checked, out checked2);
			this.checkBoxAll.Checked = @checked;
			this.checkBoxNull.Checked = checked2;
			this.isInit = false;
		}

		public ViewList(List<Level> _leveList, Level _originLevel)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Location = new System.Drawing.Point(100, 100);
			base.Icon = Resource.YArchitechIcon;
			this.thisOriginLevel = _originLevel;
			this.leveList = _leveList;
			this.selectedLevels = new List<Level>();
			this.BindCheckboxList();
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				base.DialogResult = DialogResult.Cancel;
			}
			return false;
		}

		public ViewList()
		{
			this.InitializeComponent();
		}

		private void BindCheckboxList()
		{
			this.groupBox1.Text = "目标楼层";
			if (this.thisOriginLevel != null)
			{
				this.groupBox1.Text = "目标楼层(当前实体层：" + this.thisOriginLevel.Name + ")";
			}
			this.clb_View.Items.Clear();
			foreach (Level level in this.leveList)
			{
				if (level.Name != this.thisOriginLevel.Name)
				{
					this.clb_View.Items.Add(level.Name);
				}
			}
		}

		private void RefreshCheckboxList()
		{
			if (this.rb_current.Checked)
			{
				for (int i = 0; i < this.clb_View.Items.Count; i++)
				{
					if (this.clb_View.Items[i].ToString() == this.thisOriginLevel.Name)
					{
						this.clb_View.Items.RemoveAt(i);
					}
				}
				return;
			}
			int num = this.leveList.FindIndex((Level val) => val.Name == this.thisOriginLevel.Name);
			if (num >= 0 && num <= this.clb_View.Items.Count)
			{
				this.clb_View.Items.Insert(num, this.thisOriginLevel.Name);
				return;
			}
			this.clb_View.Items.Add(this.thisOriginLevel.Name);
		}

		private void btn_confirm_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.clb_View.Items.Count; i++)
			{
				if (this.clb_View.GetItemChecked(i))
				{
					foreach (Level level in this.leveList)
					{
						if (level.Name == this.clb_View.Items[i].ToString())
						{
							this.selectedLevels.Add(level);
						}
					}
				}
			}
			if (this.rb_current.Checked)
			{
				this.isOnCurrentRiserPipe = true;
			}
			else
			{
				this.isOnCurrentRiserPipe = false;
			}
			if (this.cb_isRotate.Checked)
			{
				this.isRotated = true;
				try
				{
					double.Parse(this.txt_angle.Text);
				}
				catch
				{
					YJKMessageBox.Show("角度必须为数字");
					return;
				}
				this.angle = Convert.ToDouble(this.txt_angle.Text);
			}
			else
			{
				this.isRotated = false;
			}
			base.DialogResult = DialogResult.OK;
		}

		private void btn_cancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void rb_current_CheckedChanged(object sender, EventArgs e)
		{
			this.RefreshCheckboxList();
		}

		public List<Autodesk.Revit.DB.View> selectedViews { get; set; }

		public bool isOnCurrentRiserPipe { get; set; }

		public bool isRotated { get; set; }

		public double angle { get; set; }

		public List<Level> selectedLevels { get; set; }

		private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
		{
			if (this.isInit)
			{
				return;
			}
			if (this.checkBoxAll.Checked)
			{
				for (int i = 0; i < this.clb_View.Items.Count; i++)
				{
					this.clb_View.SetItemChecked(i, true);
				}
				this.checkBoxNull.Checked = false;
			}
		}

		private void checkBoxNull_CheckedChanged(object sender, EventArgs e)
		{
			if (this.isInit)
			{
				return;
			}
			if (this.checkBoxNull.Checked)
			{
				for (int i = 0; i < this.clb_View.Items.Count; i++)
				{
					this.clb_View.SetItemChecked(i, false);
				}
				this.checkBoxAll.Checked = false;
			}
		}

		private void cb_isRotate_CheckedChanged(object sender, EventArgs e)
		{
			this.txt_angle.Enabled = this.cb_isRotate.Checked;
		}

		private void CheckSelectedOption(out bool IsAllSelected, out bool IsNullSelected)
		{
			IsAllSelected = true;
			IsNullSelected = true;
			if (this.clb_View.Items.Count <= 0)
			{
				IsAllSelected = false;
				IsNullSelected = false;
				return;
			}
			for (int i = 0; i < this.clb_View.Items.Count; i++)
			{
				if (!this.clb_View.GetItemChecked(i) & IsAllSelected)
				{
					IsAllSelected = false;
				}
				if (this.clb_View.GetItemChecked(i) & IsNullSelected)
				{
					IsNullSelected = false;
				}
			}
		}

		private void clb_View_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.isInit)
			{
				return;
			}
			bool @checked;
			bool checked2;
			this.CheckSelectedOption(out @checked, out checked2);
			this.checkBoxAll.Checked = @checked;
			this.checkBoxNull.Checked = checked2;
		}

		private List<Autodesk.Revit.DB.View> thisViewList;

		private List<Level> leveList;

		private Level thisOriginLevel;

		private Autodesk.Revit.DB.View thisOriginView;

		private bool isInit;
	}
}
